<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入订单摘要关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          查询
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-card class="box-card">
      <el-radio-group v-model="orderStatus" size="mini" @input="checkedStatus">
        <el-radio-button :label="[]">所有</el-radio-button>
        <el-radio-button :label="['IN_PROGRESS']">未审核</el-radio-button>
        <el-radio-button :label="['SUCCESS', 'FAIL', 'BUY_FAIL', 'BUYING']">
          已审核
        </el-radio-button>
      </el-radio-group>
      <el-table v-loading="loading" border :data="dataList">
        <el-table-column label="订单编号" align="center" prop="sn" />
        <el-table-column label="用户名" align="center" prop="nickName" />
        <el-table-column label="订单摘要" align="center" prop="orderAbstract" />
        <el-table-column label="金额" align="center" prop="updatePrice" />
        <el-table-column label="下单时间" align="center" prop="createTime" />
        <el-table-column label="订单状态" align="center" prop="orderStatus">
          <template slot-scope="scope">
            <span>{{ scope.row.orderStatus | ctOrderStatusAudits }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="orderStatus">
          <template slot-scope="scope">
            <span>{{ scope.row.orderStatus | ctAuditsStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              v-if="scope.row.orderStatus == 'IN_PROGRESS'"
              @click="changeAuditsStatus(scope.row.orderItemId, 'SUCCESS')"
            >
              通过
            </el-button>
            <el-button
              size="mini"
              type="text"
              v-if="scope.row.orderStatus == 'IN_PROGRESS'"
              @click="changeAuditsStatus(scope.row.orderItemId, 'FAIL')"
            >
              拒绝
            </el-button>
            <el-button size="mini" type="text" @click="handleView(scope.row)">
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="详情" :visible.sync="detailShow" width="50%" :close-on-click-modal="false">
      <el-form :model="detailRow" label-width="100px">
        <div class="flex">
          <div>
            <el-form-item label="订单编号">
              {{ detailRow.sn || "-" }}
            </el-form-item>
            <el-form-item label="金额"> {{ detailRow.updatePrice || 0 }}元 </el-form-item>
            <el-form-item label="优惠金额">
              {{ detailRow.discountPrice || 0 }}元
            </el-form-item>
            <el-form-item label="自动续费">
              {{ detailRow.autoRenew ? "是" : "否" }}
            </el-form-item>
            <el-form-item label="订单状态">
              {{ detailRow.orderStatus | ctOrderStatusAudits }}
            </el-form-item>
          </div>
          <div>
            <el-form-item label="用户名">
              {{ detailRow.nickName || "-" }}
            </el-form-item>
            <el-form-item label="原价">
              {{ detailRow.originalPrice || 0 }}元
            </el-form-item>
            <!-- <el-form-item label="折扣">
              {{ detailRow.discount ? detailRow.discount + "折" : "无" }}
            </el-form-item> -->
            <el-form-item label="购买方式">
              {{ detailRow.periodType | ctChargeType }}
            </el-form-item>
            <el-form-item label="购买数量">
              {{ detailRow.periodNum || 0 }}
            </el-form-item>
            <el-form-item label="审核状态">
              {{ detailRow.orderStatus | ctOrderStatus }}
            </el-form-item>
          </div>
        </div>
        <el-form-item label="订单摘要">
          {{ detailRow.orderAbstract || "无" }}
        </el-form-item>
        <el-form-item label="付款截图" v-if="detailRow.paymentVoucherAddress">
          <el-image
            :src="baseUrl + detailRow.paymentVoucherAddress"
            :preview-src-list="[baseUrl + detailRow.paymentVoucherAddress]"
            style="width: 100px; height: 70px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="detailShow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAudits, changeAudits } from "@/api/auditsManagement/audits";

export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false, // 遮罩层
      orderStatus: [], //审核状态
      total: 0, // 总条数
      dataList: [], // 费用中心表格数据
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: null, //关键字查询
      },
      detailShow: false, // 详情弹窗
      detailRow: {}, // 详情内容
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**审核通过或者不通过 */
    changeAuditsStatus(orderItemId, orderStatus) {
      this.$modal
        .confirm(`是否确认审核${orderStatus == 'SUCCESS' ? '通过' : '拒绝'}?`)
        .then(() => {
          const data = {
            orderItemId,
            orderStatus,
          };
          changeAudits(data).then((res) => {
            this.$modal.msgSuccess("已审核");
            this.getList();
          });
        })
        .catch(() => { });
    },
    /**修改选中的服务器状态 */
    checkedStatus(e) {
      this.orderStatus = e;
      this.getList();
    },
    /** 查询数据列表 */
    getList() {
      let data = {
        orderStatus: this.orderStatus,
        shoppingCarEnums: "CLOUD_MARKET",
        searchValue: this.queryParams.searchValue,
        params: {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
        },
      };
      this.loading = true;
      listAudits(data)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    // 查看详情
    handleView(row) {
      this.detailRow = Object.assign({}, row);
      this.detailShow = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss" scoped>
.flex {
  display: flex;
  > div {
    flex: 1;
  }
}
</style>
